package com.vicarica.parse;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.Type;
import java.util.Collection;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.vicarica.GoogleCalendarUtils;
import com.vicarica.entity.User;

public class BirthdayParser {

	private Collection<User> readJSON(String filePath) throws ParseException, FileNotFoundException, IOException {
		
		/*Gson gson=new Gson();
		Type collectionType = new TypeToken<Collection<User>>(){}.getType();
		//fromJson = gson.fromJson(new FileReader(filePath), User.class);
		fromJson = gson.fromJson(new FileReader(filePath), collectionType);
		System.out.println(fromJson);
		return null;*/
		
		JSONParser parser=new JSONParser();
		Object result= parser.parse(new FileReader(filePath));
		JSONObject array=(JSONObject)result;
		System.out.println("User" +array.get("user"));
		
		JSONArray arrayU=(JSONArray)array.get("user");
		System.out.println("Array U :"+arrayU);

		Gson gson=new Gson();
		Type collectionType = new TypeToken<Collection<User>>(){}.getType();
		//fromJson = gson.fromJson(new FileReader(filePath), User.class);
		Collection<User> users = gson.fromJson(arrayU.toJSONString(), collectionType);
		System.out.println("Users" +users);

		return users;
	}
	
	public static void main(String[] args) {
		BirthdayParser bdayParser=new BirthdayParser();
		try {
			Collection<User> users = bdayParser.readJSON("D:/projects/VCRC/src/vicarica.json");
			GoogleCalendarUtils utils=new GoogleCalendarUtils();
			utils.addAllBirthdays(users);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}

	
}
